import { Injectable, Inject } from '@nestjs/common';

import { getConnection, Repository} from 'typeorm';
import { InjectRepository } from "@nestjs/typeorm"
import { Works } from "./entities/works.entity"
import { worksInte } from "./interface/index"
import { utils } from '../config/config.utils';
import { My,Staff } from '../my/entities/my.entity';
import {Identity} from "../my/entities/identity.entity"
@Injectable()
export class WorksService {
    constructor(
        @InjectRepository(Works) private works: Repository<Works>,
        @InjectRepository(My) private my:Repository<My>,
        @InjectRepository(Staff) private staff:Repository<Staff>,
        @InjectRepository(Identity) private identity:Repository<Identity>,
        @Inject("utils") private utils:utils   
    ) { }
    async addWorks(data: worksInte) {

        let works = new Works()
        works.content = data.content
        works.image = data.image
        works.video = data.video
        works.type = data.type
        works.site = data.site
        works.userId = data.userId
        works.worksLimit = data.worksLimit
        works.taskId = this.utils.orderCode()
        // works.userInfo_id = data.userinfo_id
        let resData = await this.works.save(works)
        return {
            code: 200,
            mes: "上传成功"
        }
    }

    //普通获取作品数据
    async getAllWorks(){
        let resData = await this.works.find()
        resData.forEach(item=>{
            item.createTime = this.utils.dateConversion( item.createTime) as unknown as Date
        })
        return {
            code:200,
            mes:"获取成功",
            data:resData
        }
    }

    // 获取团队里的数据
    async getteamData(teamId:string,examine:number){
        
        let res = await this.my.createQueryBuilder("user")
        .innerJoinAndSelect("user.works","Works",).where("Works.teamId=:teamId",{teamId})
        .andWhere("Works.examine=:examine",{examine})
        .getMany()

        if(res.length>0){
            // res.forEach(item=>{
            //     item.createTime = this.utils.dateConversion( item.createTime) as unknown as Date
            // })
            
            return {
                code:200,
                mes:"获取数据成功",
                data:this.utils.worksFormData(res)
            }
        }else{
            return {
                code:200,
                mes:"没有数据哟",
                data:[]
            }
        }
            
        // return {

        // }
    }

    // 后台管理获取的数据
    async getAllworksData(data){
        // console.log(data,"data")
    
        let skipNum = data.take *(data.current - 1)
        let pageNum = data.take
        let resData = await this.works.createQueryBuilder("user")
        .leftJoinAndSelect("user.my","works").
        leftJoinAndMapOne("user.staff",Staff,"staff","user.taskPersonId=staff.staffId").
        leftJoinAndMapOne("staff.identity",Identity,"identity","staff.staffId=identity.staff_id")
        .where("user.state=:state",{state:data.state}).skip(skipNum).take(pageNum)
       .getManyAndCount()

       resData[0].forEach(item=>{
        item.createTime = this.utils.dateConversion( item.createTime) as unknown as Date
       })
     
      if( resData[0].length>0){
        return {
            code:200,
            mes:"获取数据成功",
            data:resData[0],
            count:resData[1]
        }
      }else{
        return {
            code:400,
            mes:"获取数据失败",
            data:[]
        }
      }

    }

    // 获取我的任务
    async getMyTask(data){
         // console.log(data,"data")
    
         let skipNum = data.take *(data.current - 1)
         let pageNum = data.take
         let resData = await this.works.createQueryBuilder("user")
         .leftJoinAndSelect("user.my","works").
         leftJoinAndMapOne("user.staff",Staff,"staff","user.taskPersonId=staff.staffId").
         leftJoinAndMapOne("staff.identity",Identity,"identity","staff.staffId=identity.staff_id")
         .where("user.state=:state",{state:data.state}).andWhere("user.taskPersonId=:staffId",{staffId:data.staffId}).skip(skipNum).take(pageNum)
        .getManyAndCount()
 
        resData[0].forEach(item=>{
         item.createTime = this.utils.dateConversion( item.createTime) as unknown as Date
        })
      
       if( resData[0].length>0){
         return {
             code:200,
             mes:"获取数据成功",
             data:resData[0],
             count:resData[1]
         }
       }else{
         return {
             code:400,
             mes:"获取数据失败",
             data:[]
         }
       }
    }

    async handlesBulkData(data){
        // console.log(data.worksList)
        let totalArr = JSON.parse(JSON.stringify(data.worksList))
        // console.log(totalArr)
        data.worksList.map((item,index)=>{
            totalArr.shift()
            // console.log(index)
            // console.log(totalArr.length,"wowo")
            this.updateWorks(data.staffId,item,data.state,data.examine)
           
        })

        if(totalArr.length == 0){
            // console.log(totalArr.length,"没有数据了")
        }

        return {
            code:200,
            mes:"更新数据库成功"
        }
    }


    // 一条一条更新数据库
    async updateWorks(taskPersonId,obj,state,examine){
       let res = await this.works.createQueryBuilder().
       update("works").set({
            state:state,
            taskPersonId:taskPersonId,
            examine:examine
        }).where("id=:id",{id:obj.id}).execute()

        // console.log(res,"res")
        // console.log(obj.id)
        // console.log(taskPersonId,obj,state,"这是一条数据")
    }

}
